/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: UITextField.java 38 2008-05-07 20:00:46Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class UITextField extends UIElement
{
    private StringBuffer _text = new StringBuffer();
    private int _maxTextLength = Integer.MAX_VALUE;
    private int _cursorBlinkInterval = 200;
    private int _currentCursorBlinkTime = 0;
    private boolean _cursorVisible = true;
    private int _cursorX = 0;
    private boolean requestedCharAdded = false;

    public UITextField(int px, int py, int width, int height)
    {
        super(px, py, width, height);
    }

    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        if (hasKeyboardFocus() && _cursorVisible) {
            g.setColor(getForegroundColor());
            g.drawLine(
                    screenX + _cursorX, screenY,
                    screenX + _cursorX, screenY + getHeight()
            );
        }
        g.drawString(_text.toString(), screenX, screenY);
    }
    
    protected boolean update(int delta, GameContainer container)
        throws SlickException
    {
        _currentCursorBlinkTime += delta;
        
        if (_currentCursorBlinkTime > _cursorBlinkInterval) {
            _cursorVisible = !_cursorVisible;
            _currentCursorBlinkTime =
                _currentCursorBlinkTime - _cursorBlinkInterval;
        }
        return false;
    }


    public boolean keyPressed(int key, char c)
    {
        if (requestedCharAdded == false)
        {
            if (key == Input.KEY_DELETE)
            {
                //TODO: enable deleting via delete key

            } else if (key == Input.KEY_BACK && _text.length() > 0) {
                _text.deleteCharAt(_text.length() - 1);
                
            } else if (key == Input.KEY_ENTER) {
                UIManager.releaseKeyboardFocus(this);

            } else if (_text.length() < _maxTextLength) {
                //TODO: a text input filter
                _text.append(c);
            }

            _cursorX = getFont().getWidth(_text.toString());
    }
        
        if (key != Input.KEY_LSHIFT && key != Input.KEY_RSHIFT) {
            requestedCharAdded = true;
        }
        
        return true;
    }
    
    public boolean keyReleased(int key, char c)
    {
        requestedCharAdded = false;
        return true;
    }


    protected boolean mousePressed(int button, int x, int y)
    {
        UIManager.requestKeyboardFocus(this);
        return true;
    }

    public void setMaxTextLength(int maxTextLength)
    {
        _maxTextLength = maxTextLength;
    }

    public void setCursorBlinkInterval(int cursorBlinkInterval)
    {
        _cursorBlinkInterval = cursorBlinkInterval;
    }
    
    public String getText()
    {
        return _text.toString();
    }

    
    

}
